from PySide6.QtCore import QObject, Signal

class DictSignal(QObject):
    value_changed = Signal(str, object, object)

    def __init__(self):
        super().__init__()
        self._dict = {}

    def set_value(self, key, value):
        if key in self._dict:
            old_value = self._dict[key]
        else:
            old_value = None

        self._dict[key] = value
        self.value_changed.emit(key, old_value, value)

# 创建一个 DictSignal 实例
dict_signal = DictSignal()

# 定义一个槽函数，用于处理信号
def on_value_changed(key, old_value, new_value):
    print(f"Value for key '{key}' changed from '{old_value}' to '{new_value}'")

# 将槽函数连接到信号
dict_signal.value_changed.connect(on_value_changed)

# 模拟更新字典值
dict_signal.set_value('a', 1)
dict_signal.set_value('b', 2)
dict_signal.set_value('a', 1)
